package com.tannm.doan.taskmanager.database;

import java.util.*;

import com.tannm.doan.taskmanager.RepeatData;

public class DataRowAppointment extends DataRow {
	// Table field indexes for field select speedup
	public static class Fid {
		public static final int ID = 0;
		public static final int SUBJECT = 1;
		public static final int START_DATE = 2;
		public static final int DURATION_IN_MINUTES = 3;
		public static final int ALL_DAY = 4;
		public static final int ALARM = 5;
		public static final int REPEAT_TYPE = 6;
		public static final int REPEAT_EVERY = 7;
		public static final int REPEAT_END_ON_DATE = 8;
	};

	// Table definition
	private final DataField[] TableDef = {
			new DataField(Fid.ID, "_ID", DataField.Type.INT, true, true),
			new DataField(Fid.SUBJECT, "Subject", DataField.Type.TEXT, true,
					false),
			new DataField(Fid.START_DATE, "StartDate", DataField.Type.INT, true,
					false),
			new DataField(Fid.DURATION_IN_MINUTES, "DurationInMinutes",
					DataField.Type.INT, true, false),
			new DataField(Fid.ALL_DAY, "AllDay", DataField.Type.BOOL, true,
					false),
			new DataField(Fid.ALARM, "Alarm", DataField.Type.BOOL, true, false),
			new DataField(Fid.REPEAT_TYPE, "RepeatType", DataField.Type.INT,
					true, false),
			new DataField(Fid.REPEAT_EVERY, "RepeatEvery", DataField.Type.INT,
					true, false),
			new DataField(Fid.REPEAT_END_ON_DATE, "RepeatEndOnDate",
					DataField.Type.INT, true, false), };

	// fields
	private String sSubject = "";
	private Calendar calDateStart = Calendar.getInstance();
	private Calendar calDateStop = Calendar.getInstance();
	private int iDurationInMinutes = 15;
	private boolean bAllDay = false;
	private boolean bAlarm = true;
	private RepeatData Repeat = new RepeatData();

	// methods
	public DataRowAppointment(Database userdb) {
		super(userdb);
		setTableDefinition(TableDef);
	}

	// setters
	public void setSubject(String value) {
		sSubject = new String(value.trim());
	}

	public void setStartDate(Calendar calDate) {
		calDateStart.setTimeInMillis(calDate.getTimeInMillis());
		calDateStart.set(Calendar.SECOND, 0);
		calDateStart.set(Calendar.MILLISECOND, 0);
	}

	public void setDuration(long value) {
		iDurationInMinutes = (int) value;
	}

	public void setAllDay(boolean value) {
		bAllDay = value;
	}

	public void setAlarm(boolean value) {
		bAlarm = value;
	}

	// getters
	public String getSubject() {
		return sSubject;
	}

	public Calendar getStartDate() {
		return calDateStart;
	}

	public int getDuration() {
		return iDurationInMinutes;
	}

	public Calendar getStopDate() {
		calDateStop.setTimeInMillis(calDateStart.getTimeInMillis());
		calDateStop.add(Calendar.MINUTE, iDurationInMinutes);
		return calDateStop;
	}

	public boolean getAllDay() {
		return bAllDay;
	}

	public boolean getAlarm() {
		return bAlarm;
	}

	public RepeatData getRepeat() {
		return Repeat;
	}

	/*
	 * public String toString() { String s = ""; s += sSubject + "\n";
	 * 
	 * SimpleDateFormat dateFormatFull = new
	 * SimpleDateFormat("EEEE, dd-MM-yyyy, hh:mm");
	 * dateFormatFull.format(calDateStart);
	 * 
	 * s += DateFormat.format("EEEE, dd-MM-yyyy, hh:mm",
	 * calDateStart).toString() + "\n";
	 * 
	 * s += String.format("allday: %b, alarm: %b", bAllDay, bAlarm) + "\n"; s +=
	 * String.format("repeat: %d", GetRepeat().GetRepeatTypeAsInt()) + "\n"; s
	 * += String.format("every: %d, endon: %b", GetRepeat().GetEvery(),
	 * GetRepeat().UsingEndOnDate()) + "\n";
	 * 
	 * s += DateFormat.format("EEEE, dd-MM-yyyy, ", GetRepeat().GetEndOnDate())
	 * + "\n";
	 * 
	 * return s; }
	 */

	@Override
	public boolean validate() {
		if (sSubject.length() > 0) {
			return true;
		}
		return false;
	}

	@Override
	public void setValuesForDataRow() {
		clearContentValues();

		value(Fid.SUBJECT).set(getSubject());
		value(Fid.START_DATE).set(getStartDate());
		value(Fid.DURATION_IN_MINUTES).set(getDuration());
		value(Fid.ALL_DAY).set(getAllDay());
		value(Fid.ALARM).set(getAlarm());

		int iRepeatType = getRepeat().getRepeatTypeAsInt();

		value(Fid.REPEAT_TYPE).set(iRepeatType);
		value(Fid.REPEAT_EVERY).setNull();
		value(Fid.REPEAT_END_ON_DATE).setNull();

		// if repeat type != NONE
		if (iRepeatType != 0) {
			value(Fid.REPEAT_EVERY).set(getRepeat().getEvery());
			if (getRepeat().usingEndOnDate()) {
				value(Fid.REPEAT_END_ON_DATE).set(getRepeat().getEndOnDate());
			}
		}
	}

	@Override
	public void getValuesFromDataRow() {
		// subject values is not required for compare
		setSubject(value(Fid.SUBJECT).asString());

		setStartDate(value(Fid.START_DATE).asCalendar());
		setDuration(value(Fid.DURATION_IN_MINUTES).asLong());

		setAllDay(value(Fid.ALL_DAY).asBoolean());
		setAlarm(value(Fid.ALARM).asBoolean());

		long lRepeatType = value(Fid.REPEAT_TYPE).asLong();
		getRepeat().setRepeatTypeAsInt((int) lRepeatType);

		// if repeat type == NONE
		getRepeat().setEndOnDate(null);
		if (lRepeatType == 0) {
			getRepeat().setEvery(1);
		} else {
			getRepeat().setEvery((int) value(Fid.REPEAT_EVERY).asLong());
			if (!value(Fid.REPEAT_END_ON_DATE).isNull()) {
				Calendar calDate = value(Fid.REPEAT_END_ON_DATE).asCalendar();
				getRepeat().setEndOnDate(calDate);
			}
		}
	}

	@Override
	public String getTableName() {
		return Database.sTableNameAppointments;
	}

}
